package com.wits.av;

import android.util.Log;

import java.io.Closeable;
import java.io.IOException;

/**
 * Created by zhengboyuan on 2018-08-27.
 */

public class RecordCaster implements Closeable {

    public static final int MEDIA_EVENT_OPENING     = 0x0100;	/// 打开文件中
    public static final int MEDIA_EVENT_OPEN_FAILED = 0x0100 + 1;		/// 打开失败
    public static final int MEDIA_EVENT_OPENED  = 0x0100 + 2;			/// 打开成功
    public static final int MEDIA_EVENT_CLOSED  = 0x0100 + 3;			/// 文件关闭
    public static final int MEDIA_EVENT_END     = 0x0100 + 4;	/// 播放结束

    public static final int MEDIA_EVENT_SEEKING = 0x0100 + 6;	/// 拖拽中
    public static final int MEDIA_EVENT_SEEKED  = 0x0100 + 7;	/// 拖拽完成
    public static final int MEDIA_EVENT_SCALE   = 0x0100 + 8;

    public static final int MEDIA_EVENT_PLAYING = 0x0100 + 9;	/// 播放中
    public static final int MEDIA_EVENT_PAUSED  = 0x0100 + 10;  /// 暂停
    public static final int MEDIA_EVENT_STOPPED	= 0x0100 + 11;	/// 停止播放

    public static final int    INVALID_CHANNEL = -1;

    static boolean inited = false;

    public static void init() {
        JniRecordCaster.recordcaster_init();
        inited = true;
    }

    public static void quit() {
        JniRecordCaster.recordcaster_quit();
        inited = false;
    }


    public RecordCaster() {
        if (!inited) {
            init();
        }
    }

    @Override
    public void close() throws IOException {
        JniRecordCaster.recordcaster_closeRecordStore();
    }

    @Override
    protected void finalize() {
        try {
            close();
        } catch (Throwable throwable) {
            //
        }
    }


    /**
     * 打开录像库
     * @param filepath
     * @return
     */
    public boolean openRecordStore(String filepath) {
        return JniRecordCaster.recordcaster_openRecordStore(filepath);
    }

    /**
     * 关闭录像库
     */
    public void closeRecordStore() {
        JniRecordCaster.recordcaster_closeRecordStore();
    }

    /**
     * 搜索录像
     * @param query     查询
     * @param offset    偏移
     * @param records   录像结果, 需要预先分配
     * @return 录像个数
     */
    public int findRecord(MRecordQuery query, int offset, MRecord[] records) {
        return JniRecordCaster.recordcaster_findRecord(query, offset, records);
    }


    /**
     * 统计满足条件的录像数
     * @param query 查询条件
     * @return  录像数
     */
    public int countRecord(MRecordQuery query) {
        return JniRecordCaster.recordcaster_countRecord(query);
    }



    /**
     * 设置事件回调
     * @param listener
     */
    public void setEventCallback(RecordCasterListener listener) {
        JniRecordCaster.recordcaster_setEventCallback(listener);
    }

    /**
     * 打开通道
     * @param transport  媒体传输
     * @param records   录像文件
     * @param offset    播放偏移, 单位为毫秒
     * @param scale     播放速度, 取值范围[1/8,16]
     * @return 通道句柄, 0 表示失败
     */
    public int openFile(RecordCasterTranport transport, MRecord[] records, int offset, double scale) {
        return JniRecordCaster.recordcaster_openChannelFile(transport, records, offset, scale);
    }

    /**
     * 打开通道
     * @param transport  媒体传输
     * @param device    设备标识
     * @param fromTime  开始时间
     * @param toTime    结束时间
     * @param scale     播放速度
     * @return
     */
    public int openTime(RecordCasterTranport transport, String device, long fromTime, long toTime, double scale) {
        return JniRecordCaster.recordcaster_openChannelTime(transport, device, fromTime, toTime, scale);
    }


    /**
     * 关闭通道
     * @param channel 通道句柄
     */
    public void close(int channel) {
        JniRecordCaster.recordcaster_closeChannel(channel);
    }

    /**
     * 添加发送目标地址
     * @param channel
     * @param ip        IP地址
     * @param port      端口
     * @return
     */
    public boolean addTarget(int channel, String ip, int port) {
        return JniRecordCaster.recordcaster_addTarget(channel, ip, port);
    }

    /**
     * 删除发送目标地址
     * @param channel    通道句柄
     * @param ip        IP地址
     * @param port      端口
     * @return
     */
    public boolean removeTarget(int channel, String ip, int port) {
        return JniRecordCaster.recordcaster_removeTarget(channel, ip, port);
    }

    /**
     * 删除所有发送目标
     * @param channel    通道句柄
     * @return
     */
    public void removeTargets(int channel) {
        JniRecordCaster.recordcaster_removeTargets(channel);
    }

    /**
     * 设置播放速度
     * @param scale 播放速度, 取值范围[1/8,16]
     * @return
     */
    public boolean setScale(int channel, double scale) {
        return JniRecordCaster.recordcaster_setScale(channel, scale);
    }

    /**
     * 拖拽定位
     * @param offset    播放偏移, 单位为毫秒
     * @return
     */
    public boolean seek(int channel, int offset) {
        return JniRecordCaster.recordcaster_seek(channel, offset);
    }


    /**
     * 开始
     * @param channel
     * @return
     */
    public boolean start(int channel) {
        return JniRecordCaster.recordcaster_start(channel);
    }

    /**
     * 停止
     * @param channel
     */
    public void stop(int channel) {
        JniRecordCaster.recordcaster_stop(channel);
    }

    public void pause(int channel) {
        JniRecordCaster.recordcaster_pause(channel);
    }




}
